package com.hspedu.newData.dmsxl.树;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-26 
 * @Version: 1.0
 */

public class LC173二叉搜索树迭代器 {

    class BSTIterator {

        private int idx;
        private List<Integer> list;

        public BSTIterator(TreeNode root) {

            idx = 0;
            list = new ArrayList<>();
            inorderTraversal(root);
        }

        public int next() {
            return list.get(idx++);
        }

        public boolean hasNext() {
            return idx < list.size();
        }

        private void inorderTraversal(TreeNode treeNode){
            if (treeNode==null){
                return;
            }

            inorderTraversal(treeNode.left);
            list.add(treeNode.val);
            inorderTraversal(treeNode.right);
        }
    }
}
